// internal/repository/filerepository.go
package repository

import (
	"context"
	"gdrive/internal/model"

	"github.com/jmoiron/sqlx"
)

type FileRepository interface {
	CreateFile(ctx context.Context, file *model.File) error
	GetFilesByPath(ctx context.Context, userID, path string) ([]*model.File, error)
}

// 示例实现
type fileRepository struct {
	db *sqlx.DB
}

func NewFileRepository(db *sqlx.DB) FileRepository {
	return &fileRepository{db: db}
}

func (r *fileRepository) CreateFile(ctx context.Context, file *model.File) error {
	query := `
        INSERT INTO files (id, name, path, size, mime_type, created_at, updated_at)
        VALUES (:id, :name, :path, :size, :mime_type, :created_at, :updated_at)`
	_, err := r.db.NamedExecContext(ctx, query, file)
	return err
}

func (r *fileRepository) GetFilesByPath(ctx context.Context, userID, path string) ([]*model.File, error) {
	var files []*model.File
	query := `SELECT * FROM files WHERE owner_id = $1 AND path LIKE $2 || '%' AND deleted_at IS NULL`
	err := r.db.SelectContext(ctx, &files, query, userID, path)
	return files, err
}
